<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 出租订单管理
 *
 * @icon fa fa-circle-o
 */
class RentalOrder extends Backend
{

    /**
     * RentalOrder模型对象
     * @var \app\admin\model\RentalOrder
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\RentalOrder;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 确认出租
     * @param $orderId
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function confirmRental($ids = null){
        $orderId =$ids;
        $rentalOrder = new \app\admin\model\RentalOrder();
        $order = $rentalOrder->where('id',$orderId)->find()->toArray();
        if(!$order){
            $this->error(__('No results were found'));
        }
        // 判断权限
        $adminIds = $this->getDataLimitAdminIds();
        if(is_array($adminIds)){
            if(!in_array($order[$this->dataLimitField],$adminIds)){
                $this->error(__('You have no permission'));
            }
        }
        if($order['status']=='renting'){
            $this->error('订单已是租赁中，不用重复操作');
        }

        $rentalOrder->isUpdate(true)->save(['id' => $orderId, 'status' => 'renting']);
        $this->success('操作成功');
    }

    /**
     * 还车
     */
    public function confirmFinish($ids = null){
        $orderId =$ids;
        $rentalOrder = new \app\admin\model\RentalOrder();
        $order = $rentalOrder->where('id',$orderId)->find()->toArray();
        if(!$order){
            $this->error(__('No results were found'));
        }
        // 判断权限
        $adminIds = $this->getDataLimitAdminIds();
        if(is_array($adminIds)){
            if(!in_array($order[$this->dataLimitField],$adminIds)){
                $this->error(__('You have no permission'));
            }
        }
        if($order['status']=='complete'){
            $this->error('订单已完成，不用重复操作');
        }
        $carId = $order['cars_id'];
        $startDate = $order['startdate'];
        $endDate = $order['enddate'];
        // 启动事务
        Db::startTrans();
        try {
            $saveData = ['id' => $orderId, 'status' => 'complete','returncardate'=>date('Y-m-d')];
            $rentalOrder->isUpdate(true)->save($saveData);
            $rentalOrder->addCarRepertory($carId,$startDate,$endDate);
            // 提交事务
            Db::commit();

        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error('操作失败', $e->getMessage());
        }
        $this->success('操作成功');
    }
}
